package com.example.dzbsh.service;


import com.example.dzbsh.dao.ChaheQingkuangMapper;
import com.example.dzbsh.model.ChaheQingkuang;
import com.example.dzbsh.model.ChaheQingkuangExample;
import com.example.dzbsh.model.Jiashu;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ChaheQingkuangService {
    @Resource
    ChaheQingkuangMapper ddMapper;
    @Resource
    DeptService ddDeptService;
    @Resource
    JingshangQingkuangService ddJingshangQingkuangService;
    @Resource
    JiashuService ddJiashuService;

    public boolean add(ChaheQingkuang ppChaheQingkuang) {
        return ddMapper.insert(ppChaheQingkuang)==1;
    }

    public ChaheQingkuang findOne(String ppID){
        return ddMapper.selectByPrimaryKey(ppID);
    }

    public boolean modify(ChaheQingkuang ppQingkuang) {
        return ddMapper.updateByPrimaryKey(ppQingkuang)==1;
    }

    public List<ChaheQingkuang> findByJingshangId (String ppJingshangId){
        ChaheQingkuangExample mmExample = new ChaheQingkuangExample();
        mmExample.createCriteria().andJingshangidEqualTo(ppJingshangId);
        return  ddMapper.selectByExample(mmExample);
    }

    public List<ChaheQingkuang> findByShenfenzhenghaoAndQiyename (String ppShenfenzhenghao,String ppQiyename){
        ChaheQingkuangExample mmExample = new ChaheQingkuangExample();
        mmExample.createCriteria().andShenfenzhenghaoEqualTo(ppShenfenzhenghao).andQiyenameEqualTo(ppQiyename);
        return  ddMapper.selectByExample(mmExample);
    }

    public  List<Map<String,Object>> findByDeptId (String ppDeptId){
      return  ddMapper.findCaheByDeptId(ppDeptId);
    }

    public boolean delete(String ppID){
        return ddMapper.deleteByPrimaryKey(ppID)==1;
    }

    @Transactional
    public boolean addAndDeleteOld(ChaheQingkuang ppChaheQingkuang){

        String mmShenfenzhenghao=ppChaheQingkuang.getShenfenzhenghao()== null ?"":ppChaheQingkuang.getShenfenzhenghao().toString();
        String mmQiyename = ppChaheQingkuang.getQiyename()== null ?"":ppChaheQingkuang.getQiyename().toString();
        String mmShehuixinyongdaima = ppChaheQingkuang.getText1()== null ?"":ppChaheQingkuang.getText1().toString();
        String mmRenyuanId="";
        String mmJiashuId ="";

        // 判断是否已导入过该查核数据
        // 通过经商ID，查询查核数据
        List<ChaheQingkuang> mmChaheList = findByShenfenzhenghaoAndQiyename(mmShenfenzhenghao,mmQiyename);

        // 如果有就循环删掉

        if (mmChaheList.size()>0){
            for (int i = 0; i < mmChaheList.size(); i++) {
                ChaheQingkuang mmChaheQingkuang =mmChaheList.get(i);
                if (mmChaheQingkuang!=null){
                    String mmChaheId = mmChaheQingkuang.getId() == null ?"":mmChaheQingkuang.getId();
                    boolean mmDeleteResult = delete(mmChaheId);
                    if (!mmDeleteResult) {
                        throw new RuntimeException("旧查核情况删除失败");
                    }
                }
            }


        }

        // 身份证号和企业名查询经商情况
        List<Map<String,Object>> mmJingshangQingkuangList = ddJingshangQingkuangService.findbyshenfenzhenghaoandShehuixinyongdaima(mmShenfenzhenghao,mmShehuixinyongdaima);
        String mmShifouyujingshangYizhi = "1";
        if (mmJingshangQingkuangList.size()==1){

            // 取该家属该企业的第一条数据
            Map<String,Object> mmJingshangQingkuang = mmJingshangQingkuangList.get(0);
            if (mmJingshangQingkuang!=null){

                // 获取经商ID、家属ID、人员ID
                String mmJingshangId = mmJingshangQingkuang.get("id") == null ?"":mmJingshangQingkuang.get("id").toString();
                mmJiashuId = mmJingshangQingkuang.get("jiashuid") == null ?"":mmJingshangQingkuang.get("jiashuid").toString();
                mmRenyuanId = mmJingshangQingkuang.get("renyuanid") == null ?"":mmJingshangQingkuang.get("renyuanid").toString();

                String mmChaheLeixingText = ppChaheQingkuang.getLeixingtext()==null ?"":ppChaheQingkuang.getLeixingtext();
                String mmChaheQiyename = ppChaheQingkuang.getQiyename()==null ?"":ppChaheQingkuang.getQiyename();
                String mmChaheJingyingfanwei = ppChaheQingkuang.getJingyingfanwei() ==null ?"":ppChaheQingkuang.getJingyingfanwei();
                String mmChaheJingyingzhuangtaiText = ppChaheQingkuang.getJingyingzhuangtaitext() ==null ?"": ppChaheQingkuang.getJingyingzhuangtaitext();
                String mmChaheRenjiaochuzie = ppChaheQingkuang.getRenjiaochuzie() ==null ?"": ppChaheQingkuang.getRenjiaochuzie();
                String mmChaheZhiwuText = ppChaheQingkuang.getZhiwutext() ==null ?"": ppChaheQingkuang.getZhiwutext();
                String mmChaheQianwuShangpin = ppChaheQingkuang.getQianwushangpinhuofuwuname() ==null ?"": ppChaheQingkuang.getQianwushangpinhuofuwuname();
                String mmChaheQianwujiaoyi = ppChaheQingkuang.getQianwujiaoyiduixiangname() ==null ?"": ppChaheQingkuang.getQianwujiaoyiduixiangname();
                String mmChaheShifouzhijieguanxi = ppChaheQingkuang.getShifouyouzhijieguanxi() ==null ?"": ppChaheQingkuang.getShifouyouzhijieguanxi().toString();
                String mmChaheShijunduimingcheng = ppChaheQingkuang.getShijunduimingcheng() ==null ?"": ppChaheQingkuang.getShijunduimingcheng();
                String mmChaheJinsanzhijieguanxi = ppChaheQingkuang.getJinsanzhijieguanximingcheng() ==null ?"": ppChaheQingkuang.getJinsanzhijieguanximingcheng();
                String mmChaheShehuixinyongdaima = ppChaheQingkuang.getText1() ==null ?"": ppChaheQingkuang.getText1();

                String mmJingshangLeixingText = mmJingshangQingkuang.get("leixingtext") == null ?"":mmJingshangQingkuang.get("leixingtext").toString();
                String mmJingshangQiyename = mmJingshangQingkuang.get("qiyename") == null ?"":mmJingshangQingkuang.get("qiyename").toString();
                String mmJingshangJingyingfanwei = mmJingshangQingkuang.get("jingyingfanwei") == null ?"":mmJingshangQingkuang.get("jingyingfanwei").toString();
                String mmJingshangJingyingzhuangtaiText = mmJingshangQingkuang.get("jingyingzhuangtaitext") == null ?"":mmJingshangQingkuang.get("jingyingzhuangtaitext").toString();
                String mmJingshangRenjiaochuzie = mmJingshangQingkuang.get("renjiaochuzie") == null ?"":mmJingshangQingkuang.get("renjiaochuzie").toString();
                String mmJingshangZhiwuText = mmJingshangQingkuang.get("zhiwutext") == null ?"":mmJingshangQingkuang.get("zhiwutext").toString();
                String mmJingshangQianwuShangpin = mmJingshangQingkuang.get("qianwushangpinhuofuwuname") == null ?"":mmJingshangQingkuang.get("qianwushangpinhuofuwuname").toString();
                String mmJingshangQianwujiaoyi = mmJingshangQingkuang.get("qianwujiaoyiduixiangname") == null ?"":mmJingshangQingkuang.get("qianwujiaoyiduixiangname").toString();
                String mmJingshangShifouzhijieguanxi = mmJingshangQingkuang.get("shifouyouzhijieguanxi") == null ?"":mmJingshangQingkuang.get("shifouyouzhijieguanxi").toString();
                String mmJingshangShijunduimingcheng = mmJingshangQingkuang.get("shijunduimingcheng") == null ?"":mmJingshangQingkuang.get("shijunduimingcheng").toString();
                String mmJingshangJinsanzhijieguanxi = mmJingshangQingkuang.get("jinsanzhijieguanximingcheng") == null ?"":mmJingshangQingkuang.get("jinsanzhijieguanximingcheng").toString();
                String mmJIngshangShehuixinyongdaima = mmJingshangQingkuang.get("shehuixinyongdaima") == null ?"":mmJingshangQingkuang.get("shehuixinyongdaima").toString();


                if (!mmChaheLeixingText.equals(mmJingshangLeixingText)||!mmChaheQiyename.equals(mmJingshangQiyename)||!mmChaheJingyingfanwei.equals(mmJingshangJingyingfanwei)||
                !mmChaheJingyingzhuangtaiText.equals(mmJingshangJingyingzhuangtaiText)||!mmChaheRenjiaochuzie.equals(mmJingshangRenjiaochuzie)||!mmChaheZhiwuText.equals(mmJingshangZhiwuText)||
                !mmChaheQianwuShangpin.equals(mmJingshangQianwuShangpin)||!mmChaheQianwujiaoyi.equals(mmJingshangQianwujiaoyi)||!mmChaheShifouzhijieguanxi.equals(mmJingshangShifouzhijieguanxi)||
                !mmChaheShijunduimingcheng.equals(mmJingshangShijunduimingcheng)||!mmChaheJinsanzhijieguanxi.equals(mmJingshangJinsanzhijieguanxi)||
                !mmChaheShehuixinyongdaima.equals(mmJIngshangShehuixinyongdaima)){
                    mmShifouyujingshangYizhi="0";
                }



                ppChaheQingkuang.setJingshangid(mmJingshangId);

            }
        }else {
            mmShifouyujingshangYizhi = "0";
            List<Jiashu> mmJiashuList = ddJiashuService.findByShengfenzhenghao(mmShenfenzhenghao);
            if (mmJiashuList.size()!=1){
//                throw new RuntimeException("家属数据错误");
            }else {
                Jiashu mmJiashu = mmJiashuList.get(0);
                mmRenyuanId = mmJiashu.getRenyuanid() == null ? "":mmJiashu.getRenyuanid();
                mmJiashuId = mmJiashu.getJiashuid() == null ? "":mmJiashu.getJiashuid();
            }

        }
        ppChaheQingkuang.setJiashuid(mmJiashuId);
        ppChaheQingkuang.setRenyuanid(mmRenyuanId);
        ppChaheQingkuang.setId(UUID.randomUUID().toString());
        ppChaheQingkuang.setText2(mmShifouyujingshangYizhi);

        boolean mmAddResult = add(ppChaheQingkuang);
        if (!mmAddResult){
            throw new RuntimeException("添加查核数据失败");
        }
        return true;
    }

    public List<Map<String,Object>> findByDeptIdAndJiashunameAndShifoubangding(String ppDeptId, String ppJiashuname, String ppShifoubangding,int ppStartIndex, int ppPageSize){
        int mmStartIndex = (ppStartIndex - 1) * ppPageSize;
        String mmJiashuname = "%"+ppJiashuname+"%";
        return ddMapper.findByDeptIdAndJiashunameAndShifoubangding(ppDeptId,mmJiashuname,ppShifoubangding,mmStartIndex,ppPageSize);
    }

    public Integer getCountByDeptIdAndJiashunameAndShifoubangding(String ppDeptId, String ppJiashuname, String ppShifoubangding,int ppStartIndex, int ppPageSize) {
        int mmStartIndex = (ppStartIndex - 1) * ppPageSize;
        String mmJiashuname = "%"+ppJiashuname+"%";
        Object mmCount = ddMapper.getCountByDeptIdAndJiashunameAndShifoubangding(ppDeptId,mmJiashuname,ppShifoubangding,mmStartIndex,ppPageSize);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }


    // 获得指定单位及其下属单位的查核数量
    public int findCaheNumByDeptId(String ppDeptId,boolean ppIsAll){
        List<String> mmDeptIdList = new ArrayList<>();
        mmDeptIdList.add(ppDeptId);
        if(ppIsAll){
            mmDeptIdList.addAll(ddDeptService.findXiajiByDeptId(ppDeptId));
        }
        Object mmCount = ddMapper.findCaheNumByDeptIdList(mmDeptIdList);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    // 获得指定单位及其下属单位仍需导入的查核数量
    public int findWeiBnagdingCaheNumByDeptIdList(String ppDeptId,boolean ppIsAll){
        List<String> mmDeptIdList = new ArrayList<>();
        mmDeptIdList.add(ppDeptId);
        if(ppIsAll){
            mmDeptIdList.addAll(ddDeptService.findXiajiByDeptId(ppDeptId));
        }
        Object mmCount = ddMapper.findWeiBnagdingCaheNumByDeptIdList(mmDeptIdList);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    // 所有查核数据的数量
    public int findByChaheNum(){
        ChaheQingkuangExample mmExample = new ChaheQingkuangExample();
        mmExample.createCriteria();
        return  ddMapper.selectByExample(mmExample).size();
    }

    // 家属查核与经商不一致列表
    public List<ChaheQingkuang> findByJiashuIdAndYujingshangbuyizhi(String ppJiashuId){
        ChaheQingkuangExample mmExample = new ChaheQingkuangExample();
        mmExample.createCriteria().andJiashuidEqualTo(ppJiashuId).andText2EqualTo("0").andIsvalidEqualTo(1);
        return  ddMapper.selectByExample(mmExample);
    }

    // 未绑定查核数据的经商数据num
    public int findWeiBnagdingCaheNum(){
        Object mmCount = ddMapper.findWeiBnagdingCaheNum();
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public Integer countByJiashuId(String ppJiashuId) {
        Object mmCount = ddMapper.countByJiashuId(ppJiashuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public List<ChaheQingkuang> findByJiashuIdAndJingshangIdNull (String ppJiashuId){
        ChaheQingkuangExample mmExample = new ChaheQingkuangExample();
        mmExample.createCriteria().andJiashuidEqualTo(ppJiashuId).andJingshangidIsNull();
        return  ddMapper.selectByExample(mmExample);
    }



    public List<ChaheQingkuang> findByJiashuId (String ppJiashuId){
        ChaheQingkuangExample mmExample = new ChaheQingkuangExample();
        mmExample.createCriteria().andJiashuidEqualTo(ppJiashuId);
        mmExample.setOrderByClause("JINGSHANGID ASC");
        return  ddMapper.selectByExample(mmExample);
    }
}
